const { resolve } = require('path')

const { jgcl } = require(resolve(__dirname, '../../utils/utils'))

const Categories = require('../../database/dbModule/classify')

// 查询分类
const categorySelect = async (ctx) => {
  const data = ctx.query
  const {
    categoryName = null,
    pageNum = 1, // 页数
    pageSize = 10, // 数据条数
  } = data

  try {

    const num = await Categories.find({ isDel: false }).count()

    if (categoryName === null || categoryName === '') {
      const findRes = await Categories.find({ isDel: false })
        .skip(pageSize * (pageNum - 1))
        .limit(pageSize)

      if (findRes === null) {
        jgcl(ctx, 2015, '分类数据为空!')
      } else {
        jgcl(ctx, 200, '查询成功!', { total: num, list: findRes })
      }
    } else {
      const findRes = await Categories.find(
        {
          categoryName: { $regex: new RegExp(categoryName) },
          isDel: false
        }
      ).skip(pageSize * (pageNum - 1))
        .limit(pageSize)


      if (findRes.length === 0) {
        jgcl(ctx, 2012, '分类数据为空!')
      } else {
        jgcl(ctx, 200, '查询成功!', { total: num, list: findRes })
      }
    }

  } catch (err) {
    jgcl(ctx, 2014, '查询失败!', err)
  }
}

// 查询分类数量
const categorySelectTotal = async (ctx) => {
  try {
    const num = await Categories.find({ isDel: false }).count()

    jgcl(ctx, 200, '查询成功!', { total: num })
  } catch (err) {
    jgcl(ctx, 2014, '查询失败!', { err })

  }
}

// 新增分类
const categoryAdd = async (ctx) => {
  const data = ctx.request.body
  const {
    categoryName,
    createTime = new Date().getTime(),
    updateTime = 0,
    categoryNum = 0
  } = data

  try {

    const findRes = await Categories.findOne({ categoryName, isDel: false })

    if (findRes !== null) {
      jgcl(ctx, 2010, '分类已存在!', categoryName)
    } else {
      const createRes = await Categories.create({
        categoryName,
        createTime,
        updateTime,
        categoryNum,
        isDel: false
      })
      jgcl(ctx, 200, '新增成功!')
    }

  } catch (err) {
    const rej = Object.keys(err.errors)[0]
    if (rej === 'categoryName') {
      jgcl(ctx, 2009, '分类添加失败!', err.errors[rej].properties.message)
    } else {
      jgcl(ctx, 2011, '分类添加失败!', err)
    }
  }

}

// 删除分类
const categoryDel = async (ctx) => {
  const { id } = ctx.request.body

  try {
    const findRes = await Categories.findOne({ _id: id, isDel: false })
    if (findRes === null) {
      jgcl(ctx, 2012, '分类不存在!')
    } else {
      const delRes = await Categories.updateOne({ _id: id }, { isDel: true })
      jgcl(ctx, 200, '删除成功!')
    }

  } catch (err) {
    jgcl(ctx, 2016, '删除分类失败!', err)

  }

}

// 修改分类
const categoryUp = async (ctx) => {
  const data = ctx.request.body
  const {
    id,
    categoryName,
    updateTime = new Date().getTime()
  } = data

  try {

    const findRes = await Categories.findOne({ _id: id, isDel: false })

    if (findRes === null) {
      jgcl(ctx, 2012, '分类不存在!')
    } else {
      const upRes = await Categories.updateOne(
        { _id: id, isDel: false },
        { categoryName, updateTime }
      )
      if (upRes.modifiedCount >= 1) {
        jgcl(ctx, 200, '修改成功!')
      } else {
        jgcl(ctx, 2013, '修改失败!')

      }
    }

  } catch (err) {
    jgcl(ctx, 2013, '修改失败!', { err })
  }

}

// 查询一种分类
const categorySelectOne = async => {
  const { title } = ctx.query
}

module.exports = {
  categoryAdd,
  categoryDel,
  categoryUp,
  categorySelect,
  categorySelectTotal,
  categorySelectOne
}